import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';

class Flutter_Lottie_Page extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return Flutter_Lottie_State();
  }
}

class Flutter_Lottie_State extends State<Flutter_Lottie_Page>
    with TickerProviderStateMixin {
  AnimationController controller;

  @override
  void initState() {
    super.initState();
    controller = AnimationController(vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          leading: InkWell(
            child: Icon(Icons.arrow_back),
            onTap: () {
              Navigator.of(context).pop();
            },
          ),
          title: Text("lottie动画"),
        ),
        body: Column(
          children: [
            Container(
              child: Lottie.asset('anim/loading.json',
                  height: 100, width: 100,reverse: true),
              color: Colors.red,
            ),
            Lottie.asset(
              'anim/loading.json',
              controller: controller,
              width: 60,
              height: 100,
              repeat: true,
              onLoaded: (composition) {
                controller..duration = composition.duration;
              },
            ),
            Row(
              children: [
                ElevatedButton(
                  child: Text("开始"),
                  onPressed: () {
                    controller.reset();
                    controller.forward();
                  },
                ),
                ElevatedButton(
                  child: Text("从暂停处开始"),
                  onPressed: () {
                    controller.forward();
                  },
                ),
                ElevatedButton(
                  child: Text("停止"),
                  onPressed: () {
                    controller.stop(canceled: false);
                  },
                ),
              ],
            )
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    controller.dispose();
  }
}
